package com.zxy.study.base.recyclerview;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author : Shonn.zhang
 * @Date : 2020/7/23 11:09
 * @Desc :  适用于多种ViewHolder的场景
 */
public abstract class BaseMultipleTypeAdapter extends BaseAdapter<RecyclerData, BaseMultipleTypeVH> {

    public static final int TYPE_DEFAULT = 0x23642;

    private List<RecyclerView.ViewHolder> mAttachedHolderList = new LinkedList<>();

    public BaseMultipleTypeAdapter() {
    }

    public BaseMultipleTypeAdapter(List<RecyclerData> dataList) {
        super(dataList);
    }

    @Override
    public void onDefaultBindViewHolder(BaseMultipleTypeVH holder, RecyclerData item, int position) {
        holder.onBind(item.getData(), item.isSel(), position);
    }

    @Override
    public int getDefaultItemViewType(int position) {
        return getItemData(position).getType();
    }

    public List<RecyclerView.ViewHolder> getAttachedHolderList() {
        return mAttachedHolderList;
    }

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
        if (holder instanceof BaseMultipleTypeVH) {
            ((BaseMultipleTypeVH) holder).onViewRecycled();
        }
    }

    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        mAttachedHolderList.add(holder);

        if (holder instanceof BaseMultipleTypeVH) {
            ((BaseMultipleTypeVH) holder).onViewAttachedToWindow();
        }
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        mAttachedHolderList.remove(holder);

        if (holder instanceof BaseMultipleTypeVH) {
            ((BaseMultipleTypeVH) holder).onViewDetachedFromWindow();
        }

    }
}
